﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WmWebSystem.DbHelper;
using WmWebSystem.Models;

namespace WmWebSystem.Service
{
    public static class RandomNumberTo
    {
        //--Generate a random number--And Check the number

        private static char[] constant =   
        {   
            '0','1','2','3','4','5','6','7','8','9',  
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',   
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'   
        };
        public static string GenerateRandomNumber()
        {
            //string dNow = DateTime.Now.ToString().Trim().Replace("-", "").Replace(":", "").Replace(" ", "");
            string NoDate = DateTime.Now.ToString("yyMMdd");
            int Length = 6;

            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(62)]);
            }
            string No = "N" + NoDate + newRandom.ToString();

            while (CheckRandomNumber(No))
            {
                GenerateRandomNumber();
            }

            return No;

        }


        private static char[] constantother = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        public static string RandomNumberToName()
        {
            int Length = 12;

            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(10);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constantother[rd.Next(10)]);
            }
            string No = newRandom.ToString();
            return No;

        }

        public static bool CheckRandomNumber(string pno)
        {
            WmDb db = new WmDb();
            int count = (from p in db.Projects
                         where p.ProjectNo == pno
                         select p.ProjectNo).Count();

            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    }
}
